﻿using HcyWebAPI.Models;
using Microsoft.EntityFrameworkCore;

namespace HcyWebAPI
{
    public class MyDbContext : DbContext
    {
        public DbSet<Account> Account { get; set; }
        public DbSet<Dept> Dept { get; set; }

        public string DbPath { get; private set; }
        public string connectStr = GetConnectionString(); //"server=localhost;userid=root;password=root;database=hcywebapidb;";


        private static string GetConnectionString()
        {

            var builder = new ConfigurationBuilder()
                   .SetBasePath(Directory.GetCurrentDirectory())
                   .AddJsonFile("appsettings.json");
            var config = builder.Build();
            var commonurl = config["ConnectionStrings"];
            return commonurl;
        }

        public MyDbContext()
        {
            var folder = Environment.SpecialFolder.LocalApplicationData;
            var path = Environment.GetFolderPath(folder);
            DbPath = connectStr;

        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseMySql(connectStr, MySqlServerVersion.LatestSupportedServerVersion);
        }

        // controller 使用方式
        // var contextOptions = new DbContextOptionsBuilder<MyDbContext>().UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test").Options;
        // var context = new ApplicationDbContext(contextOptions);

        /// <summary>
        /// 传入option 使用
        /// </summary>
        /// <param name="options"></param>
        public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
        {

        }
    }
}
